package com.ygnn.reggie.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ygnn.reggie.common.CustomException;
import com.ygnn.reggie.dto.DishDto;
import com.ygnn.reggie.dto.SetmealDto;
import com.ygnn.reggie.entity.Setmeal;
import com.ygnn.reggie.entity.SetmealDish;
import com.ygnn.reggie.mapper.SetmealMapper;
import com.ygnn.reggie.service.SetmealDishService;
import com.ygnn.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    public void saveWithDish(SetmealDto setmealDto) {

//        基本数据加到setmeal表
        setmealService.save(setmealDto);
        Long id = setmealDto.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //        查询套餐状态，确认是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = setmealService.count(queryWrapper);
        if (count>0){
//            抛异常
            throw new CustomException("套餐正在售卖，不能删除");
        }
//            删除stemeal表
        setmealService.removeByIds(ids);
//            删除stemeal_dish表
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }



    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
//        更新setmeal表
        this.updateById(setmealDto);

//        根据套餐id删除setmeal_dish表里面原先的数据
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

//        在setmeal_dish表里面插入对应数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

//        遍历设置套餐id
        List<SetmealDish> list=setmealDishes.stream().map((item)->{
//            SetmealDish
            SetmealDish setmealDish=new SetmealDish();
            BeanUtils.copyProperties(item,setmealDish);

            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;

        }).collect(Collectors.toList());

        setmealDishService.saveBatch(list);

    }









}
